package com.cgwx.systemservice.controller;


import com.cgwx.commonutils.result.SysResult;
import com.cgwx.springsecurity.annotation.Log;
import com.cgwx.springsecurity.enums.BusinessType;
import com.cgwx.systemservice.entity.sys.SysMenu;
import com.cgwx.systemservice.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author GaoWei
 * @since 2023-10-12
 */

@Api(tags = "菜单表前端控制器")
@RestController
@RequestMapping("/system/sysMenu")
@CrossOrigin
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    //菜单列表（树形）
    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    @ApiOperation("菜单列表")
    @GetMapping("findNodes")
    public SysResult findNodes() {
        List<SysMenu> list = sysMenuService.findNodes();
        return SysResult.ok(list);
    }

    //添加菜单
    @Log(title = "添加菜单", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    @ApiOperation("添加菜单")
    @PostMapping("save")
    public SysResult save(
            @ApiParam(name = "sysMenu", value = "待添加的菜单")
            @RequestBody SysMenu sysMenu) {
        sysMenuService.save(sysMenu);
        return SysResult.ok();
    }

    //根据id查询菜单
    @ApiOperation("根据id查询菜单")
    @GetMapping("findNodeById/{id}")
    public SysResult findNodeById(
            @ApiParam(name = "id", value = "待查询的菜单id")
            @PathVariable String id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        return  SysResult.ok(sysMenu);
    }

    //修改菜单
    @Log(title = "修改菜单", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @ApiOperation("修改菜单")
    @PostMapping("update")
    public SysResult update(
            @ApiParam(name = "sysMenu", value = "待修改的菜单")
            @RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return SysResult.ok();
    }

    //删除菜单
    @Log(title = "删除菜单", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @ApiOperation("删除菜单")
    @DeleteMapping("remove/{id}")
    public SysResult remove(
            @ApiParam(name = "id", value = "待删除的菜单id")
            @PathVariable String id) {
        sysMenuService.removeMenuById(id);
        return SysResult.ok();
    }
}

